# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=gegl
pkgbase="mingw-w64-${_realname}"
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-docs")
pkgver=0.4.62
pkgrel=1
pkgdesc="Generic Graphics Library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://gegl.org/"
msys2_repository_url="https://gitlab.gnome.org/GNOME/gegl"
msys2_references=(
  'archlinux: gegl'
  "cpe: cpe:/a:gegl:gegl"
)
license=('spdx:GPL-3.0-or-later AND LGPL-3.0-or-later')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-asciidoc"
             "${MINGW_PACKAGE_PREFIX}-gobject-introspection"
             "${MINGW_PACKAGE_PREFIX}-gi-docgen"
             "${MINGW_PACKAGE_PREFIX}-gettext-tools"
             $([[ ${CARCH} == aarch64 ]] || echo "${MINGW_PACKAGE_PREFIX}-luajit")
             "${MINGW_PACKAGE_PREFIX}-vala")
depends=("${MINGW_PACKAGE_PREFIX}-babl"
         "${MINGW_PACKAGE_PREFIX}-cairo"
         "${MINGW_PACKAGE_PREFIX}-ffmpeg"
         "${MINGW_PACKAGE_PREFIX}-gexiv2"
         "${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gdk-pixbuf2"
         "${MINGW_PACKAGE_PREFIX}-gettext-runtime"
         "${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-jasper"
         "${MINGW_PACKAGE_PREFIX}-json-glib"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libraw"
         "${MINGW_PACKAGE_PREFIX}-librsvg"
         "${MINGW_PACKAGE_PREFIX}-libspiro"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-lcms2"
         "${MINGW_PACKAGE_PREFIX}-lensfun"
         "${MINGW_PACKAGE_PREFIX}-maxflow"
         "${MINGW_PACKAGE_PREFIX}-omp"
         "${MINGW_PACKAGE_PREFIX}-openexr"
         "${MINGW_PACKAGE_PREFIX}-pango"
         "${MINGW_PACKAGE_PREFIX}-poppler"
         "${MINGW_PACKAGE_PREFIX}-SDL2"
         $([[ ${CARCH} == i686 ]] || echo "${MINGW_PACKAGE_PREFIX}-suitesparse"))
noextract=("${_realname}-${pkgver}.tar.xz")
source=(https://download.gimp.org/pub/gegl/${pkgver%.*}/${_realname}-${pkgver}.tar.xz
        "0001-disable-codeview.patch")
sha256sums=('5887576371ebf1d9e90797d10e4b9a7f1658228d4827583e79e1db3d94505c6c'
            'f1e5d02bb7e57ead285f3d9417670aafe664e766e20c24f40ee95e6e8f90d404')

prepare() {
  tar -xf "${_realname}-${pkgver}.tar.xz" || true
  tar -xf "${_realname}-${pkgver}.tar.xz" || true

  cd "${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}/0001-disable-codeview.patch"
}

build() {
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  meson setup \
    --prefix="${MINGW_PREFIX}" \
    --wrap-mode=nodownload \
    --buildtype=plain \
    --auto-features=enabled \
    -Dlibv4l=disabled \
    -Dlibv4l2=disabled \
    -Dmrg=disabled \
    -Dpygobject=disabled \
    -Dsdl1=disabled \
    -Dintrospection=true \
    -Ddocs=false \
    $([[ ${CARCH} == aarch64 ]] && echo "-Dlua=disabled") \
    $([[ ${CARCH} == i686 ]] && echo "-Dumfpack=disabled") \
    ../${_realname}-${pkgver}

  meson compile
}

package_gegl() {
  meson install -C build-${MSYSTEM} --destdir "${pkgdir}"

  rm "${pkgdir}${MINGW_PREFIX}"/lib/gegl-*/*.dll.a

  # split docs
  mkdir -p dest${MINGW_PREFIX}/share
  mv "${pkgdir}${MINGW_PREFIX}"/share/doc dest${MINGW_PREFIX}/share/doc
}

package_gegl-docs() {
  pkgdesc+=" (documentation)"
  depends=()

  mv dest/* "${pkgdir}"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
